package com.ghy.www.my.loadbalance.consumer.controller;

import com.ghy.www.api.IService1;
import com.ghy.www.api.IService2;
import com.ghy.www.api.IService3;
import com.ghy.www.api.IService4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class TestController {
    @Autowired
    private IService1 service1;
    @Autowired
    private IService2 service2;
    @Autowired
    private IService3 service3;
    @Autowired
    private IService4 service4;

    @RequestMapping("Test1")
    public String test1() {
        String helloString = service1.getHello("中国人1");
        System.out.println("public String test1() " + helloString);
        return "返回信息：" + helloString;
    }

    private int test2_provider1_runtime = 0;
    private int test2_provider2_runtime = 0;
    private int test2_provider3_runtime = 0;
    private int test2_allRuntime = 0;

    @RequestMapping("Test2")
    public String test2() {
        String helloString = service2.getHello("中国人2");
        System.out.println("public String test2() " + helloString);
        int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
        System.out.println(runPort);
        switch (runPort) {
            case 8085:
                test2_provider1_runtime++;
                break;
            case 8086:
                test2_provider2_runtime++;
                break;
            case 8087:
                test2_provider3_runtime++;
                break;
        }
        test2_allRuntime++;
        System.out.println("test2_allRuntime=" + test2_allRuntime);
        if (test2_allRuntime == 10) {
            System.out.println("test2_provider1_runtime=" + test2_provider1_runtime);
            System.out.println("test2_provider2_runtime=" + test2_provider2_runtime);
            System.out.println("test2_provider3_runtime=" + test2_provider3_runtime);
            test2_provider1_runtime = 0;
            test2_provider2_runtime = 0;
            test2_provider3_runtime = 0;
            test2_allRuntime = 0;
        }
        return "返回信息：" + helloString;
    }

    private int test3_provider1_runtime = 0;
    private int test3_provider2_runtime = 0;
    private int test3_provider3_runtime = 0;

    @RequestMapping("Test3")
    public void test3(HttpServletRequest request, HttpServletResponse response) {
        for (int i = 0; i < 5000; i++) {
            String helloString = service3.getHello("中国人3");
            System.out.println("public String test3() " + helloString);
            int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
            System.out.println(runPort + " " + (i + 1) + "次运行");
            switch (runPort) {
                case 8085:
                    test3_provider1_runtime++;
                    break;
                case 8086:
                    test3_provider2_runtime++;
                    break;
                case 8087:
                    test3_provider3_runtime++;
                    break;
            }
        }
        System.out.println("test3_provider1_runtime=" + test3_provider1_runtime);
        System.out.println("test3_provider2_runtime=" + test3_provider2_runtime);
        System.out.println("test3_provider3_runtime=" + test3_provider3_runtime);
    }

    private int test4_provider1_runtime = 0;
    private int test4_provider2_runtime = 0;
    private int test4_provider3_runtime = 0;

    @RequestMapping("Test4")
    public void test4(HttpServletRequest request, HttpServletResponse response) {
        {
            String helloString = service4.getHello("中国人4_1");
            System.out.println("public String test4() " + helloString);
            int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
            addRuntime(runPort);
        }
        {
            String helloString = service4.getHello("中国人4_1");
            System.out.println("public String test4() " + helloString);
            int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
            addRuntime(runPort);
        }
        {
            String helloString = service4.getHello("中国人4_1");
            System.out.println("public String test4() " + helloString);
            int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
            addRuntime(runPort);
        }
        {
            String helloString = service4.getHello("中国人4_1");
            System.out.println("public String test4() " + helloString);
            int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
            addRuntime(runPort);
        }
        {
            String helloString = service4.getHello("中国人4_1");
            System.out.println("public String test4() " + helloString);
            int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
            addRuntime(runPort);
        }
        {
            String helloString = service4.getHello("我是其它的参数值，就是为了测试hash值不一样，会导致执行不同的提供者！");
            System.out.println("public String test4() " + helloString);
            int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
            addRuntime(runPort);
        }
        System.out.println("test4_provider1_runtime=" + test4_provider1_runtime);
        System.out.println("test4_provider2_runtime=" + test4_provider2_runtime);
        System.out.println("test4_provider3_runtime=" + test4_provider3_runtime);
        test4_provider1_runtime = 0;
        test4_provider2_runtime = 0;
        test4_provider3_runtime = 0;
    }

    private void addRuntime(int runPort) {
        switch (runPort) {
            case 8085:
                test4_provider1_runtime++;
                break;
            case 8086:
                test4_provider2_runtime++;
                break;
            case 8087:
                test4_provider3_runtime++;
                break;
        }
    }
}
